/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_errno.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_adc.h"
#include "wifiiot_uart.h"

#define UART_TASK_STACK_SIZE 1024 * 8
#define UART_TASK_PRIO 25
#define UART_BUFF_SIZE 1000

// static const char *data = "Hello, BearPi!\r\n";
// static const char data_t[12] ={nullptr};
//   check sum
unsigned char checkSum(unsigned char *buff)
{
    unsigned char sum = 0;
    int i;
    for (i = 0; i < buff[2] - 3; i++)
    {
        sum += buff[i];
    }
    return sum;
}

// main
static void UART_Task(void)
{
    uint8_t uart_buff[UART_BUFF_SIZE] = {0};
    uint8_t *uart_buff_ptr = uart_buff;
    uint32_t ret;

    WifiIotUartAttribute uart_attr = {

        // baud_rate: 9600
        .baudRate = 115200,

        // data_bits: 8bits
        .dataBits = 8,
        .stopBits = 1,
        .parity = 0,
    };

    // Initialize uart driver
    ret = UartInit(WIFI_IOT_UART_IDX_1, &uart_attr, NULL);
    if (ret != WIFI_IOT_SUCCESS)
    {
        printf("Failed to init uart! Err code = %d\n", ret);
        return;
    }
    printf("UART Test Start\n");
    int left_speed_pwm_s = 901;
    int right_speed_pwm_s = -901;

    unsigned char buffer[12];
    buffer[0] = 0x53;                              // headcode1
    buffer[1] = 0x4b;                              // headcode2
    buffer[2] = 0x0c;                              // 12 all communicate num
    buffer[3] = 0x00;                              // cmd1
    buffer[4] = 0x18;                              // cmd2
    buffer[5] = (char)left_speed_pwm_s;            // data1
    buffer[6] = (char)(left_speed_pwm_s >> 8);     // data2
    buffer[7] = (char)right_speed_pwm_s;           // data3
    buffer[8] = (char)(right_speed_pwm_s >> 8);    // data4
    buffer[9] = checkSum((unsigned char *)buffer); // checksum
    buffer[10] = 0x0d;                             // endcode1
    buffer[11] = 0x0a;                             // endcode2
    UartWrite(WIFI_IOT_UART_IDX_1, &buffer[0], 12);
    usleep(1000000);

    left_speed_pwm_s = 901;
    right_speed_pwm_s = 901;
    buffer[5] = (char)left_speed_pwm_s;            // data1
    buffer[6] = (char)(left_speed_pwm_s >> 8);     // data2
    buffer[7] = (char)right_speed_pwm_s;           // data3
    buffer[8] = (char)(right_speed_pwm_s >> 8);    // data4
    buffer[9] = checkSum((unsigned char *)buffer); // checksum
    UartWrite(WIFI_IOT_UART_IDX_1, &buffer[0], 12);
    usleep(1000000);

    left_speed_pwm_s = 0;
    right_speed_pwm_s = 0;
    buffer[5] = (char)left_speed_pwm_s;            // data1
    buffer[6] = (char)(left_speed_pwm_s >> 8);     // data2
    buffer[7] = (char)right_speed_pwm_s;           // data3
    buffer[8] = (char)(right_speed_pwm_s >> 8);    // data4
    buffer[9] = checkSum((unsigned char *)buffer); // checksum
    UartWrite(WIFI_IOT_UART_IDX_1, &buffer[0], 12);
    usleep(1000000);

    while (1)
    {

        //通过串口1接收数据
        UartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);

        printf("%s", buffer);
        usleep(1000000);
    }
}

static void UART_ExampleEntry(void)
{

    osThreadAttr_t attr;

    attr.name = "UART_Task";
    attr.attr_bits = 0U;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = UART_TASK_STACK_SIZE;
    attr.priority = UART_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr) == NULL)
    {
        printf("[ADCExample] Falied to create UART_Task!\n");
    }
}

APP_FEATURE_INIT(UART_ExampleEntry);